package com.sj.parksdk.fragment.profile;

import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.animation.OvershootInterpolator;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.layout.LinearLayoutHelper;
import com.sj.parksdk.R;
import com.sj.parksdk.R2;
import com.sj.parksdk.adapter.base.delegate.SimpleDelegateAdapter;
import com.sj.parksdk.core.BaseFragment;
import com.sj.parksdk.net.http.MyNetApi;
import com.sj.parksdk.net.http.callback.TipRequestCallBack;
import com.sj.parksdk.net.request.MGetRequest;
import com.sj.parksdk.net.result.databean.Data_Coupons;
import com.sj.parksdk.setting.Extras;
import com.sj.parksdk.utils.Utils;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xrouter.annotation.AutoWired;
import com.xuexiang.xrouter.launcher.XRouter;
import com.xuexiang.xui.adapter.recyclerview.RecyclerViewHolder;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.layout.ExpandableLayout;
import com.xuexiang.xui.widget.statelayout.StatefulLayout;
import com.xuexiang.xutil.data.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;

import butterknife.BindView;

public class FragmentCoupons extends BaseFragment {
    @BindView(R2.id.recycler)
    RecyclerView recycler;
    @BindView(R2.id.ll_state)
    StatefulLayout llState;
    @AutoWired(name = Extras.COUPONS_AVAILABLE)
    boolean available;

    private SimpleDelegateAdapter<Data_Coupons> adapter;
    private DateFormat format = new SimpleDateFormat("yyyy.MM.dd");

    public static FragmentCoupons newInstance(boolean available) {
        Bundle args = new Bundle();
        args.putBoolean(Extras.COUPONS_AVAILABLE, available);
        FragmentCoupons fragment = new FragmentCoupons();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void initArgs() {
        XRouter.getInstance().inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.parksdk_layout_common_list;
    }

    @Override
    protected TitleBar initTitle() {
        return null;
    }

    @Override
    protected void initViews() {
        adapter = new SimpleDelegateAdapter<Data_Coupons>(R.layout.parksdk_adapter_coupons_avaliable, new LinearLayoutHelper()) {
            @Override
            protected void bindData(@NonNull RecyclerViewHolder holder, int position, Data_Coupons item) {
                holder.image(R.id.iv_title,ResUtils.getDrawable((item.isExpired() || item.isUsed()) ?R.drawable.park_bg_couposn_disable :R.drawable.park_bg_coupons_avaliable));
                holder.text(R.id.rtv_title,getCouponsTitle(item));
                holder.text(R.id.tv_name, item.getName());
                holder.text(R.id.tv_time,
                        DateUtils.millis2String(item.getBeginTime(), format)
                                + "-"
                                + DateUtils.millis2String(item.getEndTime(), format));
                holder.visible(R.id.iv_state, (item.isExpired() || item.isUsed()) ? View.VISIBLE : View.GONE);
                if (item.isUsed())
                    holder.image(R.id.iv_state, ResUtils.getDrawable(R.drawable.park_ic_coupons_used));
                if (item.isExpired())
                    holder.image(R.id.iv_state, ResUtils.getDrawable(R.drawable.park_ic_coupons_outtime));
                holder.text(R.id.tv_rem, item.getRemark());
                ExpandableLayout exl = (ExpandableLayout) holder.findViewById(R.id.expandable_rem);
                holder.click(R.id.ll_rem, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (exl.isExpanded()) exl.collapse();
                        else exl.expand();
                    }
                });
                exl.setInterpolator(new OvershootInterpolator(1.0f));
                AppCompatImageView ivIndicator = holder.findViewById(R.id.iv_expand);
                exl.setOnExpansionChangedListener((expansion, state) -> {
                    if (ivIndicator != null) {
                        ivIndicator.setRotation(expansion * 180);
                    }
                });
            }
        };
        WidgetUtils.initRecyclerView(recycler, DensityUtils.dp2px(10), ResUtils.getColor(R.color.base_bg_color));
        recycler.setAdapter(adapter);
    }

    private Spanned getCouponsTitle(Data_Coupons item) {
        switch (item.getPattern()) {
            case 0:
                return Utils.getPriceHtml(item.getValue().toString(), 0);
            case 1:
                return Html.fromHtml("<big>" + item.getValueStr() + "</big><small>小时</small>");
            case 2:
                return Html.fromHtml("单次");
        }
        return Html.fromHtml("--");
    }

    @Override
    protected void initData() {
        queryCoupons();
    }

    private void queryCoupons() {
        llState.showLoading();
        new MGetRequest(MyNetApi.USER_COUPONS)
                .user()
                .params(MyNetApi.Coupons.STATE,available?"effective":"ineffective")
                .params(MyNetApi.PageParam.pageIndex,1)
                .params(MyNetApi.PageParam.pageSize,100)
                .execute(new TipRequestCallBack<Data_Coupons>() {
                    @Override
                    public void onSuccess(Data_Coupons data) throws Throwable {
                        queryCouponsSuccess(data);
                    }

                    @Override
                    public void onError(ApiException e) {
                        super.onError(e);
                        llState.showError(e.getMessage(),null);
                    }
                });
    }

    private void queryCouponsSuccess(Data_Coupons data) {
        if(data == null || data.getItems() == null || data.getItems().size() == 0){
            llState.showCustom(emptyStatus(R.drawable.park_ic_empty_coupons));
            return;
        }else llState.showContent();
        adapter.refresh(data.getItems());
    }
}
